//
//  SDSharedNibController.m
//  SuperDoctor
//
//  Created by 年浩杰 on 16/7/9.
//  Copyright © 2016年 SD. All rights reserved.
//

#import "SDSharedNibViewController.h"
#import "SDSKTagView.h"

@interface SDSharedNibViewController ()<UIActionSheetDelegate,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
//上传头像
@property (nonatomic,strong) UIButton *iconButton;
//文本标签
@property (nonatomic,strong) UITextField *nameText;
//选择标签
@property (nonatomic,strong) UIView *tagView;




@end

@implementation SDSharedNibViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    //初始化控件
    [self setupFrame];
}
#pragma mark ---初始化控件
- (void)setupFrame {
    
    __weak typeof(self) weakSelf = self;
  //实例化
    UIButton *iconBtn = [[UIButton alloc] init];
    //设置图片
    [iconBtn setBackgroundImage:[UIImage imageNamed:@"illness_img_person.png"] forState:UIControlStateNormal];
    //监听点击方法
    [iconBtn addTarget:self action:@selector(btnActionForEditPortrait:) forControlEvents:UIControlEventTouchDown];
    //添加
    [self.view addSubview:iconBtn];
    //赋值
    self.iconButton = iconBtn;
    
    //上传头像的约束
    [self.iconButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.size.mas_equalTo(CGSizeMake(90, 90));
        make.centerX.mas_equalTo(weakSelf.view);
        make.top.width.offset(90);
        
    }];
    //实例化
    UILabel *iconLabel = [[UILabel alloc] init];
    //设置颜色
    iconLabel.textColor = [UIColor lightGrayColor];
    //设置文字
    iconLabel.text = @"上传社团头像";
    //设置大小
    iconLabel.font = FONT(15);
    //添加
    [self.view addSubview:iconLabel];
    //头像文字的约束
    [iconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(iconBtn);
        make.top.equalTo(iconBtn.mas_bottom).with.offset(20);
    }];
    //社团编辑
    UIImageView *editIcon = [[UIImageView alloc] init];
    //设置图片
    editIcon.image = [UIImage imageNamed:@"product_img_icon_xieyi.png"];
    //添加
    [self.view addSubview:editIcon];
    //编辑约束
    [editIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(25, 20));
        make.left.equalTo (weakSelf.view).offset(10);
        make.top.equalTo(iconLabel.mas_bottom).offset(30);
        
    }];
    
    UITextField *nameText = [[UITextField alloc]init];
    
    nameText.placeholder = @"请填写社区名(社团名最多6个字)";
    
    [self.view addSubview:nameText];
    
    self.nameText = nameText;
    
    [self.nameText mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_equalTo(20);
        
        make.centerY.equalTo(editIcon);
        
        make.right.equalTo(weakSelf.view).offset(-10);
        
        make.left.equalTo(editIcon.mas_right).offset(5);
        
    }];
    
    
    UIImageView *xian = [[UIImageView alloc] init];
    
    xian.backgroundColor = [UIColor colorWithRed:226 green:226 blue:226 alpha:1];
    
    [self.view addSubview:xian];
    
    [xian mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_equalTo(1);
        
        make.left.equalTo(weakSelf.view).offset(10);
        
        make.right.equalTo(weakSelf.view).offset(-10);
        
        make.top.equalTo(editIcon.mas_bottom).offset(5);
        
    }];
    
    
    UILabel *tagLabel = [[UILabel alloc] init];
    
    tagLabel.text = @"选择标签";
    
    tagLabel.textColor = [UIColor lightGrayColor];
    
    tagLabel.font = FONT(15);
    
    [self.view addSubview:tagLabel];
    
    [tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_equalTo(20);
        
        make.width.mas_equalTo(60);
        
        make.left.equalTo(weakSelf.view).offset(10);
        
        make.top.equalTo(xian).offset(35);
        
    }];
    
    
    UITextField *tagText = [[UITextField alloc] init];
    
    tagText.placeholder = @"美容颜";
    
    tagText.borderStyle = UITextBorderStyleRoundedRect;
    
    tagText.delegate = self;
    
    [tagText addTarget:self action:@selector(textTag) forControlEvents:UIControlEventTouchDown];
    
    [self.view addSubview:tagText];
    
    [tagText mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(tagLabel);
        
        make.right.equalTo(weakSelf.view).offset(-10);
        
        make.left.equalTo(tagLabel.mas_right).offset(5);
        
    }];
    
    SDSKTagView *view = [[SDSKTagView alloc] init];
    
    view.backgroundColor = [UIColor redColor];
    
    self.tagView = view;
    
    [self.view addSubview:view];
    
    [self.tagView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(weakSelf.view).offset(10);
        
        make.right.equalTo(weakSelf.view).offset(-10);
        
        make.top.equalTo(tagText.mas_bottom).offset(10);
        
        
    }];
    
    
    
    UILabel *label = [[UILabel alloc] init];
    
    label.font = FONT(13);
    
    label.textColor = RED_COLOR;
    
    label.text = @"PS:成员和视频越多得社团越容易被发现!";
    
    [self.view addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(weakSelf.view).offset(10);
        
        make.right.equalTo(weakSelf.view).offset(-10);
        
        make.top.equalTo(view.mas_bottom).offset(115);
        
        
    }];
    
    
    UIButton *commitBtn = [[UIButton alloc] init];
    
    [commitBtn setTitle:@"  确   认   发   布  " forState:UIControlStateNormal];
    
    [commitBtn setTitleColor:RED_COLOR forState:UIControlStateNormal];
    
    commitBtn.titleLabel.font = FONT(15);
    
    [commitBtn addTarget:self action:@selector(didClickChangeButton) forControlEvents:UIControlEventTouchDown];
    
    commitBtn.layer.borderWidth = 1.0f;
    
    commitBtn.layer.cornerRadius = 5;
    
    commitBtn.layer.borderColor = [[UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1]CGColor];
    
    
    
    [self.view addSubview:commitBtn];
    
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_equalTo(30);
        
        make.left.equalTo(weakSelf.view).offset(10);
        
        make.right.equalTo(weakSelf.view).offset(-10);
        
        make.top.equalTo(label.mas_bottom).offset(20);
        
    }];

}

- (void ) didClickButton {
    
    UIActionSheet* actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"请选择文件来源"
                                delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:nil
                                  otherButtonTitles:@"照相机",@"摄像机",@"本地相簿",@"本地视频",nil];
    [actionSheet showInView:self.view];
    
   
}
- (void)btnActionForEditPortrait:(id) sender {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
    picker.delegate = self;
    
    [self presentViewController:picker animated:YES completion:nil];
    
}
//从相册中选取照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    // 取出被选择的照片
    UIImage *selectImage = info[UIImagePickerControllerOriginalImage];
    // 创建一个view, 用来展示 用户选择的图片
    [self.iconButton setImage:selectImage forState:UIControlStateNormal];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void) textTag {
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
}

- (void)didClickChangeButton {
    
    UIActionSheet* actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"请确认是否发布"
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                destructiveButtonTitle:nil
                                  otherButtonTitles:@"微信",@"新浪微博",@"QQ空间",@"微博",@"邮箱",nil];
    [actionSheet showInView:self.view];
}


@end
